package com.zry.yard.ui.activity

import android.content.Intent
import android.view.View
import com.zry.yard.R
import com.zry.yard.adapter.BatchInfoAdapter
import com.zry.yard.adapter.BatchOPeraAdapter
import com.zry.yard.animation.RecyclerViewUtilKt
import com.zry.yard.been.*
import com.zry.yard.databinding.ActivityBatchInfoBinding
import com.zry.yard.retrofit.HttpServer
import com.zry.yard.retrofit.ShowUserView
import com.zry.yard.ui.base.BaseActivity
import kotlinx.android.synthetic.main.activity_batch_info.*
import kotlinx.android.synthetic.main.common_title.*

class BatchInfoActivity : BaseActivity(), ShowUserView {
    private var batchInfoBean: BatchInfoBean? = null
    private var batchNextInfo: BatchNextInfo? = null
    private var batchInfoBinding: ActivityBatchInfoBinding? = null
    private var title: String? = null
    override fun getLayoutId(): Int {
        return R.layout.activity_batch_info
    }

    override fun initData() {
        title = intent.getStringExtra("title")

        commonLeftLinearLayout.setOnClickListener { finish() }
        batchInfoBean = intent.getSerializableExtra("batchInfoBean") as BatchInfoBean
        batchInfoBinding = viewDataBinding as ActivityBatchInfoBinding
        HttpServer(this).getBatchInfo(batchInfoBean!!.BatchID, this)

        when (title) {
            "批次创建" ->{
                titleCenterText.text = "批次详情"
                traceabilityBatchLinear.visibility = View.GONE
            }
            "溯源录入" -> {
                titleCenterText.text = "溯源录入"
                traceabilityBatchBtn.setOnClickListener {
                    if (batchNextInfo!!.DetailList.size > 0) {
                        val intent = Intent(this, TraceabilityCreateActivity::class.java)
                        intent.putExtra("batchNextInfo", batchNextInfo)
                        intent.putExtra("batchID", batchInfoBean!!.BatchID)
                        startActivity(intent)
                    } else {
                        toast("暂时没有操作节点，请在pc端创建")
                    }
                }
            }
        }
        initRecycler()
    }

    private fun initRecycler() {
        recyclerViewUtilKt = RecyclerViewUtilKt(this, batchInfoRecycler)
        recyclerViewUtilKt?.initRecyclerView()
    }

    private fun initAdapter(batchDetailList: MutableList<BatchDetail>) {
        val batchInfoAdapter = BatchInfoAdapter(R.layout.item_batch_info, batchDetailList)
        recyclerViewUtilKt?.setAdapter(batchInfoAdapter)
    }

    private fun initOpearAdapter(operaDetailList: MutableList<BatchOPera>) {
        val batchOPeraAdapter = BatchOPeraAdapter(R.layout.item_batch_opera, operaDetailList)
        recyclerViewUtilKt?.setAdapter(batchOPeraAdapter)
    }


    override fun toMainActivity(what: Int, data: Any) {
        when (what) {
            29 -> {
                val resultData = data as ResultBean<BatchNextInfo>
                if (resultData.State == "1") {
                    batchNextInfo = resultData.ResultData
                    batchInfoBinding?.batchNextInfo = resultData.ResultData
                    when (title) {
                        "批次创建" -> initAdapter(resultData.ResultData.DetailList)
                        "溯源录入" -> initOpearAdapter(resultData.ResultData.OPeraList)
                    }
                }
            }
        }
    }
}
